package com.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.action.InstrumentAction;
import com.bean.InstrumentBean;
import com.bean.MessageBean;
import com.exception.DBException;
import com.exception.InstrumentException;
import com.util.HandleInfo;
import com.util.Regex;
import com.util.StateInfo;

public class InstrumentDeleteServlet extends HttpServlet
{

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		String key = request.getParameter("key");
		String value = request.getParameter("value");
		String barcode = request.getParameter("barcode");
		
		if(null==barcode || null == key || null==value)
		{
			HandleInfo.handle(request, response,  
					StateInfo.INNER_CONFIRM_FAIL);
			return;
		}
		
		if("".equals(key) || "".equals(value) || "".equals(barcode))
		{
			HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
			return;
		}
		
		if(!"name".equals(key) && !"barcode".equals(key))
		{
			HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
			return;
		}
		
		if ("barcode".equals(key) && !Regex.checkBarcode(value))
		{
			HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
			return;
		}
		if(!Regex.checkBarcode(barcode))
		{
			HandleInfo.handle(request, response, StateInfo.INNER_CONFIRM_FAIL);
			return;
		}
		MessageBean message = new MessageBean();
		message.setKey(key);
		message.setValue(value);
		
		InstrumentBean insBean = new InstrumentBean();
		insBean.setBarcode(barcode);
		try
		{
			new InstrumentAction().deleteInstrument(request, response, insBean, message);
		}
		catch (DBException e)
		{
			HandleInfo.handle(request, response, e.getMessage());
			return;
		}
		catch (InstrumentException e)
		{
			HandleInfo.handle(request, response, e.getMessage());
			return;
		}
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		doGet(request, response);
	}

}
